package rn.automacao.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

import rn.automacao.banco.Conexao;
import rn.automacao.dominio.AcessoLogVO;
import rn.automacao.dominio.AcessoVO;

public class AcessoDAO implements ObjetoDAO {

	Connection conn = null;

	PreparedStatement ps = null;

	ResultSet rs = null;

	public void alterar(Object objeto) {
		// TODO Auto-generated method stub

	}

	public void cadastrar(Object objeto) {
		if (objeto instanceof AcessoLogVO) {
			AcessoLogVO acessolog = (AcessoLogVO) objeto;
			try {
				ps = conn.prepareStatement("INSERT INTO ACESSOLOG (CODFUNC, DATA, IP, STATUS) VALUES (?,?,?,?)");
				ps.setInt(1, acessolog.getCodfunc());
				ps.setTimestamp(2, acessolog.getData());
				ps.setString(3, acessolog.getIp());
				ps.setString(4, acessolog.getStatus());
				ps.executeUpdate();

			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (ps != null)
						ps.close();
					if (conn != null)
						conn.close();

				} catch (SQLException e) {
					e.printStackTrace();
				}

			}

		}
	}

	public void deletar(Object objeto) {
		// TODO Auto-generated method stub

	}

	public Collection<Object> listar() {
		// TODO Auto-generated method stub
		return null;
	}

	public Collection<Object> pesquisar(String param, String filtro) {
		// TODO Auto-generated method stub

		return null;
	}

	public Object pesquisar(int param, String filtro) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object pesquisarLogin(String login, String senha) {

		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT codfunc, senha FROM Acesso WHERE LOGIN=?");
			ps.setString(1, login.trim());
			rs = ps.executeQuery();

			rs.next();
			return new AcessoVO(rs.getInt("codfunc"), login, rs.getString(
					"senha").trim());

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();


			} catch (SQLException ex) {
				ex.printStackTrace();
			}
		}

		return null;
	}

}
